package com.zelin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zelin.mapper.ClassesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.zelin.pojo.Student;
import com.zelin.mapper.StudentMapper;
import com.zelin.service.StudentService;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * Created by WF on 2021/10/12 14:28
 */
@Service
public class StudentServiceImpl implements StudentService{

    @Resource
    private StudentMapper studentMapper;
    @Autowired
    private ClassesMapper classesMapper;
    @Override
    public List<Student> findAll() {
        //1. 查询所有学生
        List<Student> students = studentMapper.selectAll();
        System.out.println("students = " + students);
        //2. 学生设置班级名称
        for (Student student : students) {
            student.setCname(classesMapper.selectByPrimaryKey(student.getCid()).getCname());
        }
        //3. 返回
        return students;
    }

    /**
     * 功能: 条件查询带分页
     * 参数:
     * 返回值: com.github.pagehelper.PageInfo<com.zelin.pojo.Student>
     * 时间: 2021/10/13 16:15
     */
    @Override
    public PageInfo<Student> search(Integer page, Integer pageSize, Student student) {
        //1. 开始分页
        PageHelper.startPage(page,pageSize);
        //2. 开始查询
        //2.1 定义查询实例
        Example example = new Example(Student.class);
        //2.2 添加条件查询
        Example.Criteria criteria = example.createCriteria();
        //2.3 根据查询参数是否有值添加查询条件
        if(student != null){
            if(StrUtil.isNotBlank(student.getSname())){
                criteria.andLike("sname","%" + student.getSname() + "%");
            }
            if(StrUtil.isNotBlank(student.getAddr())){
                criteria.andLike("addr","%" + student.getAddr() + "%");
            }
            if(student.getCid() != null && student.getCid() != 0){
                criteria.andEqualTo("cid",student.getCid());
            }
        }
        //2.4 开始条件查询
        List<Student> students = studentMapper.selectByExample(example);
        //2.5 学生设置班级名称
        for (Student st : students) {
            st.setCname(classesMapper.selectByPrimaryKey(st.getCid()).getCname());
        }
        //2.6 定义PagerInfo对象
        PageInfo<Student> studentPageInfo = new PageInfo<>(students);
        //2.7 返回
        return studentPageInfo;
    }

    /**
     * 功能: 添加学生
     * 参数:
     * 返回值: void
     * 时间: 2021/10/14 15:30
     */
    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    /**
     * 功能: 修改学生
     * 参数:
     * 返回值: void
     * 时间: 2021/10/14 15:32
     */
    @Override
    public void update(Student student) {
        studentMapper.updateByPrimaryKey(student);
    }

    /**
     * 功能: 删除学生
     * 参数:
     * 返回值: void
     * 时间: 2021/10/14 15:45
     */
    @Override
    public void delete(String sid) {
        studentMapper.deleteByPrimaryKey(sid);
    }
}
